#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

void QueueInit(Queue* q)
{
	assert(q);

	q->front = NULL;
	q->rear = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	else 
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
}

void QueuePop(Queue* q)
{
	assert(q);

	if (q->front->next == NULL)
	{
		free(q->front);
		q->front = NULL;
		q->rear = NULL;
	}
	else
	{
		QNode* next = q->front;
		q->front = q->front->next;
		free(next);
	}
}

QDataType QueueFront(Queue* q)
{
	assert(q);

	return q->front->data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);

	return q->rear->data;
}

int QueueSize(Queue* q)
{
	assert(q);

	int size = 0;
	QNode* cur = q->front;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

bool QueueEmpty(Queue* q)
{
	assert(q);

	return q->front == NULL;
}

void QueueDestroy(Queue* q)
{
	QNode* cur = q->front;
	while (cur)
	{
		QNode* tmp = cur;
		cur = cur->next;
		free(tmp);
	}
	q->front = NULL;
	q->rear = NULL;
}

void Func(Queue q)
{
	;
}